﻿using System;
using System.Collections.Generic;
using System.Text;
using voidbox;
using Silk.NET.OpenGLES;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace TTEngine.Graphics
{
    public class FontTTF : ResourceObj, IFont
    {
        public int FontSize => _font.Size;

        public PackedTextureGroup PackGroup
        {
            get;
            private set;
        }
        ISystemTtfFont _font;

        Dictionary<char, PackedFontCharInfo> mapChar = new Dictionary<char, PackedFontCharInfo>();

        protected override void OnDestory()
        {

        }
        public FontTTF(ResourcePackage package, ISystemTtfFont font, PackedTextureGroup group)
            : base(package)
        {
            this.PackGroup = group;
            this._font = font;
        }
        public bool EnsureTextPacked(string text)
        {
            bool change = false;
            foreach (var c in text)
            {
                if (mapChar.ContainsKey(c))
                    continue;
                var cinfo = new PackedFontCharInfo();
                cinfo.fontadv = _font.GetCharWidth(c);
                var data = _font.GetCharImage(c, 1);
                cinfo.elem = PackGroup.AddSpriteInfo(data, SpriteEffect.GrayAsAlpha, Border.One);
                mapChar[c] = cinfo;
                change = true;
            }
            return change;

        }
        public void ApplyPackedTextureData()
        {
            this.PackGroup.ApplyTextureData();
        }
        public bool TryGetCharInfo(char c, out PackedFontCharInfo value)
        {
            return mapChar.TryGetValue(c, out value);

        }
    }
}
